package com.cloud.system.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloud.system.api.bean.LoginHumanSuggest;
import com.cloud.system.api.entity.SysInfo;
import com.cloud.system.api.entity.SysResources;
import com.cloud.system.api.enums.ResourceTypeEnum;
import com.cloud.system.api.enums.YesNoEnum;
import com.cloud.system.api.mapper.HumanInfoMapper;
import com.cloud.system.api.mapper.LoginHumanSuggestMapper;
import com.cloud.system.api.mapper.SysInfoMapper;
import com.cloud.system.api.mapper.SysResourcesMapper;
import com.cloud.system.api.service.ResourcesService;
import com.cloud.system.common.result.QkIcon;
import com.cloud.system.common.result.QkMenuItem;
import com.cloud.system.common.result.QkTreeNode;
import com.cloud.system.common.utils.MenuItemHelper;
import com.cloud.system.common.utils.RedisKeyHelper;
import com.cloud.system.common.utils.TreeHelper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ResourcesServiceImpl implements ResourcesService {

	@Autowired
	private SysResourcesMapper sysResourceMapper;

	@Autowired
	private SysInfoMapper sysInfoMapper;

	@Autowired
	private SysResourcesMapper sysResourcesMapper;

	@Autowired
	private HumanInfoMapper humanInfoMapper;

	@Autowired
	private LoginHumanSuggestMapper loginHumanSuggestMapper;

	@Override
	public QkTreeNode[] listResourcesTree() {
		// 资源
		Example example = new Example(SysResources.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andNotEqualTo("deleteFlag", 1);
		example.setOrderByClause("display_order");
		List<SysResources> sysRes = sysResourceMapper.selectByExample(example);
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		for (SysResources res : sysRes) {
			QkTreeNode node = new QkTreeNode("RES_" + res.getResId(), res.getResTitle());
			node.setSelectable(true);
			if (res.getSeniorId() == null || res.getSeniorId() <= 0) {
				node.setSeniorId("SYSTEM_" + res.getSysId());
			} else {
				node.setSeniorId("RES_" + res.getSeniorId());
			}
			node.setExpanded(true);
			node.getData().put("code", res.getResCode());
			node.getData().put("type", res.getResTypeId());
			node.setIcon(res.getDisplayIcon());
			ResourceTypeEnum e = ResourceTypeEnum.getResTypeEnum(res.getResTypeId());
			node.setType(e.getName());
			nodes.add(node);
		}
		// 系统
		Example sysExample = new Example(SysInfo.class);
		Example.Criteria sysCriteria = sysExample.createCriteria();
		sysCriteria.andNotEqualTo("deleteFlag", 1);
		sysCriteria.andEqualTo("enableFlag", 1);
		example.setOrderByClause("display_order");
		List<SysInfo> sysInfos = sysInfoMapper.selectByExample(sysExample);
		for (SysInfo info : sysInfos) {
			QkTreeNode node = new QkTreeNode("SYSTEM_" + info.getSysId(), info.getSysName());
			node.getData().put("code", "/" + info.getSysId() + "/");
			node.getData().put("type", 0);
			if (info.getSeniorId() == null || info.getSeniorId() <= 0) {
				node.setExpandedIcon(QkTreeNode.FA_FOLDER_OPEN);
				node.setCollapsedIcon(QkTreeNode.FA_FOLDER);
				node.setExpanded(true);
				node.setSelectable(false);
				node.setRootFlag(true);
				node.setLevel(0);
			} else {
				node.setIcon(QkTreeNode.FA_LAPTOP);
				node.setSelectable(true);
			}
			node.setSeniorId("SYSTEM_" + info.getSeniorId());
			nodes.add(node);
		}
		return TreeHelper.buildList2Tree(nodes);
	}

	@Override
	public QkTreeNode[] listResourcesBySysId(long sysId) {
		Example example = new Example(SysResources.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andNotEqualTo("deleteFlag", 1);
		criteria.andEqualTo("sysId", sysId);
		List<SysResources> sysRes = sysResourceMapper.selectByExample(example);
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		for (SysResources res : sysRes) {
			QkTreeNode node = new QkTreeNode(res.getResId(), res.getResTitle());
			if (res.getSeniorId() == null || res.getSeniorId() <= 0) {
				node.setExpanded(false);
				node.setRootFlag(true);
				node.setLevel(0);
				node.setSeniorId(sysId + "");
			}
			node.setSelectable(true);
			node.setExpanded(true);
			node.setSeniorId(res.getSeniorId() + "");
			ResourceTypeEnum e = ResourceTypeEnum.getResTypeEnum(res.getResTypeId());
			if (null != e)
				node.setType(e.getName());
			node.setStyleClass("ui" + e);
			nodes.add(node);
			Map<String, Object> data = node.getData();
			data.put("resTitle", res.getResTitle());
			data.put("resTypeName", res.getResTypeName());
			data.put("resUrl", res.getResUrl());
			data.put("displayIcon", res.getDisplayIcon());
		}
		return TreeHelper.buildList2Tree(nodes);
	}

	@Override
	public String deleteResource(long id) {
		SysResources sysRes = sysResourceMapper.selectByPrimaryKey(id);
		SysResources res = new SysResources();
		res.setDeleteFlag(1);
		Example example = new Example(SysResources.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andLike("resCode", sysRes.getResCode() + "%");
		sysResourceMapper.updateByExampleSelective(res, example);
		return sysRes.getResCode();
	}

	@Override
	public Long addResource(SysResources resources) {
		return null;
	}

	@Override
	public String saveUpdateResource(SysResources resource) {
		resource.setDeleteFlag(YesNoEnum.NO.getCode());
		if (null != resource.getResId() && resource.getResId() >= 0) {
			sysResourceMapper.updateByPrimaryKey(resource);
		} else {
			long resId =resource.getResId();
			resource.setResId(resId);
			if (null != resource.getSeniorId() && resource.getSeniorId() > 0) {
				SysResources parent = sysResourceMapper.selectByPrimaryKey(resource.getSeniorId());
				resource.setResCode(parent.getResCode() + "/" + resId + '/');
				resource.setSysId(parent.getSysId());
				resource.setSeniorName(parent.getResTitle());
			} else {
				SysInfo sysinfo = sysInfoMapper.selectByPrimaryKey(resource.getSysId());
				resource.setResCode("/" + sysinfo.getSysId() + "/" + resId + "/");
				if (null == resource.getDisplayOrder())
					resource.setDisplayOrder((int) resId);
				resource.setSysId(sysinfo.getSysId());
			}
			sysResourceMapper.insert(resource);
		}
		return resource.getResCode();
	}

	@Override
	public SysResources loadResource(long id) {
		SysResources res = sysResourceMapper.selectByPrimaryKey(id);
		if (null != res.getSeniorId() && res.getSeniorId() > 0) {
			SysResources seniorRes = sysResourceMapper.selectByPrimaryKey(res.getSeniorId());
			if (null != seniorRes) {
				res.setSeniorName(seniorRes.getResTitle());
			}
		} else {
			SysInfo sysInfo = sysInfoMapper.selectByPrimaryKey(res.getSysId());
			res.setSeniorName(sysInfo.getSysName());
		}
		return res;
	}

	@Override
	public SysResources showEditResourceDetail(long id, long sysId) {
		SysResources resource = new SysResources();
		resource.setSeniorId(id);
		resource.setSysId(sysId);
		if (id > 0) {
			SysResources parent = sysResourceMapper.selectByPrimaryKey(id);
			resource.setSeniorName(parent.getResTitle());
		} else {
			SysInfo info = sysInfoMapper.selectByPrimaryKey(sysId);
			resource.setSeniorName(info.getSysName());
		}
		return resource;
	}

	@Override
	public List<SysResources> listResourcesBySysAndHuman(long sysId, long humanId) {
		List<SysResources> resources = null;
		if (humanId < 0) {
			// 超級管理員用戶
			Example example = new Example(SysResources.class);
			Example.Criteria criteria = example.createCriteria();
			criteria.andEqualTo("sysId", sysId);
			criteria.andEqualTo("deleteFlag", 0);
			example.orderBy("displayOrder");
			resources = sysResourcesMapper.selectByExample(example);

		} else {
			// 普通用户
			resources = sysResourcesMapper.listResourcesBySysIdAndHumanId(sysId, humanId);
		}
		return resources;

	}

//	@Override
//	public List<LoginHumanSuggest> searchHumanName(String value) {
//		Page<LoginHumanSuggest> humanPage = PageHelper.startPage(1, 20).doSelectPage(() -> {
//			loginHumanSuggestMapper.selectlistPage("%" + value + "%");
//		});
//		return humanPage.getResult();
//	}

	@Override
	public List<LoginHumanSuggest> searchHumanName(String value ,String code) {
		Page<LoginHumanSuggest> humanPage = PageHelper.startPage(1, 20).doSelectPage(() -> {
			loginHumanSuggestMapper.selectOptionlist("%" + value + "%" ,code);
		});
		return humanPage.getResult();
	}

}
